//
//  MainUITabBarController.m
//  项目1 CLCMovie
//
//  Created by huiwenjiaoyu on 15/7/17.
//  Copyright (c) 2015年 CLC. All rights reserved.
//

#import "MainUITabBarController.h"
#import "CinemaViewController.h"
#import "HomeViewController.h"
#import "TopViewController.h"
#import "NewsViewController.h"
#import "MoreViewController.h"

@interface MainUITabBarController ()
{
    UIImageView* img;
}
@end

@implementation MainUITabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _creatViewControllers];
    [self setTabBar];

    // Do any additional setup after loading the view.
}


- (void)_creatViewControllers
{
    
    //创建第三层ViewController
    CinemaViewController* cnmVC = [[CinemaViewController alloc] init];
    HomeViewController* homeVC = [[HomeViewController alloc] init];
    TopViewController* topVC = [[TopViewController alloc] init];
    NewsViewController* newsVC = [[NewsViewController alloc] init];
    MoreViewController* moreVC = [[MoreViewController alloc] init];
    
    NSArray* vcArray = @[homeVC,newsVC,topVC,cnmVC,moreVC];
    
    //创建第二层navigationController
    NSMutableArray* navArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < 5; i++) {
        BaseNavController* nav = [[BaseNavController alloc] initWithRootViewController:vcArray[i]];
        [navArray addObject:nav];
    }
    
    self.viewControllers = navArray;
    
}


- (void)setTabBar
{
    //设置背景
    
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_bg_all"]];

    //移除原有的TabBarButton
    for (UIView* subView in self.tabBar.subviews) {
        Class cls = NSClassFromString(@"UITabBarButton");
        if ([subView isKindOfClass:cls]) {
            [subView removeFromSuperview];
            
        }
    }
    
    
    //
    CGFloat width = CGRectGetWidth(self.tabBar.frame)/5;
    CGFloat height = CGRectGetHeight(self.tabBar.frame);

    NSArray* imgName = @[@"movie_home",@"msg_new",@"start_top250",@"icon_cinema",@"more_setting"];
    NSArray* btnName = @[@"首页",@"新闻",@"top250",@"影院",@"更多"];
    
//    for (int i = 0; i < 5; i++) {
//        UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(width*i, 0, width, height)];
//        [btn setImage:[UIImage imageNamed:imgName[i]] forState:UIControlStateNormal];
//        [btn setTitle:btnName[i] forState:UIControlStateNormal];
//        
//        btn.titleLabel.font = [UIFont systemFontOfSize:11];
//        
//        btn.imageEdgeInsets = UIEdgeInsetsMake(-10, 20, 0, 0);
//        btn.titleEdgeInsets = UIEdgeInsetsMake(30, -18, 0, 0);
//   //     btn.titleEdgeInsets = UIEdgeInsetsMake(-, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
//        [self.tabBar addSubview:btn];
//    }
    img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    [img setImage:[UIImage imageNamed:@"selectTabbar_bg_all1"]];
    [self.tabBar addSubview:img];
   
    //button set
    for (int i = 0; i < 5; i++) {
        TabButton* btn = [[TabButton alloc] initWithFrame:CGRectMake(width*i, 0, width, height) withImageName:imgName[i] withTitle:btnName[i]];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:btn];
    }
    
    
}

- (void)btnAction:(UIButton*)btn
{
    NSInteger index = btn.tag;
    self.selectedIndex = index;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.3];
    img.center = btn.center;
    [UIView commitAnimations];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
